﻿using Microsoft.EntityFrameworkCore;
using SJ.Platform.Common;
using SJ.Platform.EFCore.Model;
using SJ.Platform.EFCore.Model.FlowManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SJ.Platform.EFCore.DBOperation.DBOperation.MySQL.Flow
{
    public class Flow_NodeHandle
    {
        private readonly SJDBContext _context;

        public Flow_NodeHandle(SJDBContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> CreateAsync(FlowNode entity)
        {
            try
            {
                _context.FlowNodes.Add(entity);
                await _context.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_NodeHandle=>Create", ex);
                return false;
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(FlowNode entity)
        {
            try
            {
                _context.Entry(entity).State = EntityState.Modified;
                await _context.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_NodeHandle=>Update", ex);
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(FlowNode entity)
        {
            try
            {
                _context.FlowNodes.Remove(entity);
                return _context.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_NodeHandle=>Delete", ex);
                return false;
            }
        }

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteByIdAsync(string id)
        {
            try
            {
                var entity = _context.FlowNodes.Find(id);
                if (entity != null)
                {
                    _context.FlowNodes.Remove(entity);
                    await _context.SaveChangesAsync();
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_NodeHandle=>DeleteById", ex);
            }
            return false;
        }


        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<FlowNode> GetEntityByIdAsync(string id)
        {
            try
            {
                return await _context.FlowNodes.FindAsync(id);
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_NodeHandle=>GetEntityById", ex);
            }
            return null;
        }

        /// <summary>
        /// 获取流程节点
        /// </summary>
        /// <param name="flowId"></param>
        /// <returns></returns>
        public async Task<List<FlowNode>> GeFlowNodeByFlowIdAsync(string flowId)
        {
            try
            {
                var query = _context.FlowNodes.AsQueryable();
                query = query.Where(n => n.Fid == flowId);
                query = query.OrderBy(order => order.Sort);

                // 执行查询并返回结果
                var flownodes = await query.ToListAsync();

                return flownodes;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_NodeHandle=>GetAllAsync  page", ex);
            }
            return null;
        }

        /// <summary>
        /// 获取流程启动节点
        /// </summary>
        /// <param name="flowId"></param>
        /// <returns></returns>
        public async Task<FlowNode> GeFlowStartNodeByFlowIdAsync(string flowId)
        {
            try
            {
                var query = _context.FlowNodes.AsQueryable();
                query = query.Where(n => n.Fid == flowId && n.Sort == 1);

                // 执行查询并返回结果
                return await query.FirstAsync();
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_NodeHandle=>GeFlowStartNodeByFlowIdAsync  page", ex);
            }
            return null;
        }

        /// <summary>
        /// 获取下一个审批节点
        /// </summary>
        /// <param name="flowId"></param>
        /// <param name="currentNodeId"></param>
        /// <returns></returns>
        public async Task<FlowNode> GeFlowNextNodeFromCurrentNodeIdAsync(string flowId, string currentNodeId)
        {
            try
            {
                var query = _context.FlowNodes.AsQueryable();
                query = query.Where(n => n.Fid == flowId && n.Id == currentNodeId);
                var node = await query.FirstAsync();

                // 执行查询并返回结果
                query = _context.FlowNodes.AsQueryable();
                return await query.Where(n => n.Fid == flowId && n.Sort == node.Sort + 1).FirstAsync();
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_NodeHandle=>GeFlowNextNodeFromCurrentNodeIdAsync ", ex);
            }
            return null;
        }
    }
}
